package com.lucian.ge.controller.shopportal;

import com.lucian.ge.auth.controller.GetAuthController;
import com.lucian.ge.comman.model.ResultMap;
import com.lucian.ge.service.shop.PartnerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import javax.validation.Valid;
import javax.validation.constraints.NotEmpty;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * Created by Lucicol on 2019/8/14.
 */
@Api(value = "PartnerController", description = "PartnerController-合伙人")

@RestController
@RequestMapping("/partner")
@Validated
public class PartnerController {
    private final ResultMap resultMap;

    @Autowired
    public PartnerController(ResultMap resultMap) {
        this.resultMap = resultMap;
    }

    @Autowired
    private PartnerService partnerService;

    /**
     *货款与收益查询
     */
//    @ApiOperation(value="货款与收益查询", notes="")
//    @ApiImplicitParams({
//            @ApiImplicitParam(paramType="header", name = "token", value = "token", required = false, dataType = "String")
//    })
//    @GetMapping("listIncome")
//    @ResponseBody
//    public ResultMap listIncome (@RequestHeader String token, @RequestParam(value = "userId") Integer userId) {
//        if (!GetAuthController.getPartnerAuth(token,userId).equals("合伙人权限验证通过")) {
//            return resultMap.success().code(301).message(GetAuthController.getPartnerAuth(token,userId)).token("").res("").data(null);
//        }
//        List<Map<String,String>> selectIncome= partnerService.selectIncome(userId);
//        return resultMap.success().code(200).message("获得货款与收益查询信息成功").token("").res(selectIncome).data(null);
//    }

    /**
     *货款充值与支出查询
     */
//    @ApiOperation(value="分享订单及下线分成查询", notes="")
//    @ApiImplicitParams({
//            @ApiImplicitParam(paramType="header", name = "token", value = "token", required = false, dataType = "String")
//    })
//    @GetMapping("listRecharge")
//    @ResponseBody
//    public ResultMap listRecharge (@RequestHeader String token, @RequestParam(value = "userId") Integer userId) {
//        if (!GetAuthController.getPartnerAuth(token,userId).equals("合伙人权限验证通过")) {
//            return resultMap.success().code(301).message(GetAuthController.getPartnerAuth(token,userId)).token("").res("").data(null);
//        }
//        List<Map<String,String>> selectRecharge=partnerService.selectRecharge(userId);
//        return resultMap.success().code(200).message("获得分享订单及下线分成成功").token("").res(selectRecharge).data(null);
//    }

    /**
     *货款充值与支出查询
     */
    @ApiOperation(value="货款充值与支出查询", notes="")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="header", name = "token", value = "token", required = false, dataType = "String")
    })
    @GetMapping("/listProfit")
    @ResponseBody
    public ResultMap listProfit (@RequestHeader String token, @RequestParam(value = "userId") Integer userId) {
        if (!GetAuthController.getPartnerAuth(token,userId).equals("合伙人权限验证通过")) {
            return resultMap.success().code(301).message(GetAuthController.getPartnerAuth(token,userId)).token("").res("").data(null);
        }
        List<Map<String,String>> selectProfit=partnerService.selectProfit(userId);
        return resultMap.success().code(200).message("获得货款充值与支出查询信息成功 ").token("").res(selectProfit).data(null);
    }

    /**
     * 申请成为合伙人
     * */
    @ApiOperation(value="申请成为合伙人", notes="")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="header", name = "token", value = "token", required = false, dataType = "String")
    })
    @GetMapping("/insertUserId")
    @ResponseBody
    public ResultMap insertUserId(
                                @Valid
                                @RequestHeader String token,
                                  @RequestParam(value = "userId") Integer userId,
//                                  @RequestParam(value = "shangji") Integer shangji,
//                                  @RequestParam(value = "status") Integer status,
                                  @RequestParam(value = "mingzi") @NotEmpty(message="姓名不能为空") String mingzi,
                                  @RequestParam(value = "shouji") @NotEmpty(message="手机号不能为空") String shouji,
                                  @RequestParam(value = "weixin") @NotEmpty(message="微信号不能为空") String weixin,
                                  @RequestParam(value = "shenfenzheng") @NotEmpty(message="身份证不能为空") String shenfenzheng){

        if (!GetAuthController.getUserAuth(token, userId).equals("普通注册用户验证通过")) {
            return resultMap.success().code(301).message(GetAuthController.getUserAuth(token, userId)).token("").res("").data(null);
        }

        partnerService.insertUserId(userId,mingzi,shouji,weixin,shenfenzheng);
//        return resultMap.success().code(200).message("申请成为合伙人提交成功").token("").res("").data(null);
        try {
            return resultMap.success().code(200).message("申请成为合伙人提交成功").token("").res("").data(null);
        } catch (Exception ex) {
            ex.printStackTrace();
            return resultMap.fail().code(401).message("申请成为合伙人提交失败").token("").res("").data(null);
        }
    }

    /**
     *查询是否有这个UserId, 申请合伙人
     */
    @ApiOperation(value="查询是否有这个UserId", notes="")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="header", name = "token", value = "token", required = false, dataType = "String")
    })
    @GetMapping("/selectUserId")
    @ResponseBody
    public ResultMap selectUserId (@RequestHeader String token, @RequestParam(value = "userId") Integer userId) {
        if (!GetAuthController.getUserAuth(token, userId).equals("普通注册用户验证通过")) {
            return resultMap.success().code(301).message(GetAuthController.getUserAuth(token, userId)).token("").res("").data(null);
        }
        String selectUserId=partnerService.selectUserId(userId);
        if(selectUserId==null){
            return resultMap.success().code(200).message("数据库没有这个UserId").token("").res("1").data(null);
        } else {
            return resultMap.success().code(200).message("查询UserId成功 ").token("").res("0").data(null);
        }
    }

    /**
     *查询可提现金额
     */
    @ApiOperation(value="查询可提现金额", notes="")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="header", name = "token", value = "token", required = false, dataType = "String")
    })
    @GetMapping("/selectYuE")
    @ResponseBody
    public String selectYuE (@RequestHeader String token, @RequestParam(value = "userId") Integer userId) {
//        if (!GetAuthController.getPartnerAuth(token,userId).equals("合伙人权限验证通过")) {
//            return resultMap.success().code(301).message(GetAuthController.getPartnerAuth(token,userId)).token("").res("").data(null);
//        }
        String selectYuE=partnerService.selectYuE(userId);
//        return resultMap.success().code(200).message("查询可提现金额成功 ").token("").res(selectYuE).data(null);
        return selectYuE;
    }

    /**
     * 申请提现()
     * */
    @ApiOperation(value="申请提现", notes="")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="header", name = "token", value = "token", required = false, dataType = "String")
    })
    @GetMapping("/insertTiXian")
    @ResponseBody
    public ResultMap insertTiXian(
                                   @Valid
                                    @RequestHeader String token,
                                  @RequestParam(value = "userId") Integer userId,
                                  @RequestParam(value = "tixianjine") @NotEmpty(message="金额不能为空") String tixianjine,
                                    @RequestParam(value = "name") @NotEmpty(message="姓名不能为空") String name,
                                    @RequestParam(value = "tixianhao") @NotEmpty(message="银行卡号不能为空") String tixianhao){
//        if (!GetAuthController.getPartnerAuth(token,userId).equals("合伙人权限验证通过")) {
//            return resultMap.success().code(301).message(GetAuthController.getPartnerAuth(token,userId)).token("").res("").data(null);
//        }

        String selectYuE=partnerService.selectYuE(userId);

        Double a = Double.valueOf(selectYuE);
        Double b = Double.valueOf(tixianjine);
//        System.out.println(a);
//        System.out.println(b);

        if(a >= b) {
            partnerService.insertTiXian(userId,tixianjine,name,tixianhao);
            return resultMap.success().code(200).message("申请提现提交成功").token("").res("").data(null);
        } else if(a < b){
            return resultMap.success().code(200).message("超出可提现余额").token("").res("").data(null);
        } else {
            return resultMap.success().code(200).message("请填写正确信息").token("").res("").data(null);
        }

//        if(Integer.valueOf(selectYuE) >= Integer.valueOf(tixianjine)) {
//            partnerService.insertTiXian(userId,tixianjine,name,tixianhao);
//            return resultMap.success().code(200).message("申请提现提交成功").token("").res("").data(null);
//        } else if(Integer.valueOf(selectYuE) < Integer.valueOf(tixianjine)){
//            return resultMap.success().code(200).message("超出可提现余额").token("").res("").data(null);
//        } else {
//            return resultMap.success().code(200).message("请填写正确信息").token("").res("").data(null);
//        }

//        partnerService.insertTiXian(userId,tixianjine,name,tixianhao);
//
//        return resultMap.success().code(200).message("申请提现提交成功").token("").res("").data(null);
    }

    /**
     *查询已结算金额
     */
    @ApiOperation(value="查询已结算金额", notes="")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="header", name = "token", value = "token", required = false, dataType = "String")
    })
    @GetMapping("/selectJieSuan")
    @ResponseBody
    public String selectJieSuan (@RequestHeader String token, @RequestParam(value = "userId") Integer userId) {
//        if (!GetAuthController.getPartnerAuth(token,userId).equals("合伙人权限验证通过")) {
//            return resultMap.success().code(301).message(GetAuthController.getPartnerAuth(token,userId)).token("").res("").data(null);
//        }
        String selectJieSuan=partnerService.selectJieSuan(userId);
//        return resultMap.success().code(200).message("查询已结算金额成功 ").token("").res(selectJieSuan).data(null);
        return selectJieSuan;
    }

    /**
     *查询订单,客户付款金额,佣金
     */
    @ApiOperation(value="查询订单,客户付款金额,佣金", notes="")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="header", name = "token", value = "token", required = false, dataType = "String")
    })
    @GetMapping("/selectDinDan")
    @ResponseBody
    public ResultMap selectDinDan (@RequestHeader String token, @RequestParam(value = "userId") Integer userId,@RequestParam(value = "timerang") Integer timerang) {
//        if (!GetAuthController.getPartnerAuth(token,userId).equals("合伙人权限验证通过")) {
//            return resultMap.success().code(301).message(GetAuthController.getPartnerAuth(token,userId)).token("").res("").data(null);
//        }
        List<Map<String,String>> selectDinDan=partnerService.selectDinDan(userId,timerang);
        return resultMap.success().code(200).message("查询订单,客户付款金额,佣金成功").token("").res(selectDinDan).data(null);
    }

    /**
     * 查询提现记录
     */
    @ApiOperation(value="查询提现记录", notes="")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="header", name = "token", value = "token", required = false, dataType = "String")
    })
    @GetMapping("/drawrecords")
    @ResponseBody
    public ResultMap drawRecords (@RequestHeader String token, @RequestParam(value = "userId") Integer userId) {
//        if (!GetAuthController.getPartnerAuth(token,userId).equals("合伙人权限验证通过")) {
//            return resultMap.success().code(301).message(GetAuthController.getPartnerAuth(token,userId)).token("").res("").data(null);
//        }
        List<Map<String,String>> drawRecords=partnerService.drawRecords(userId);
        return resultMap.success().code(200).message("查询提现记录成功").token("").res(drawRecords).data(null);
    }

    /**
     *查询下级收益信息
     */
    @ApiOperation(value="查询下级收益信息", notes="")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="header", name = "token", value = "token", required = false, dataType = "String")
    })
    @GetMapping("/selectXiaJiShouYi")
    @ResponseBody
    public ResultMap selectXiaJiShouYi (@RequestHeader String token,
                                        @RequestParam(value = "userId") Integer userId,
                                        @RequestParam(value = "timerang") Integer timerang) {
//        if (!GetAuthController.getPartnerAuth(token,userId).equals("合伙人权限验证通过")) {
//            return resultMap.success().code(301).message(GetAuthController.getPartnerAuth(token,userId)).token("").res("").data(null);
//        }
        List<Map<String,String>> selectXiaJiShouYi=partnerService.selectXiaJiShouYi(userId,timerang);
        return resultMap.success().code(200).message("查询查询下级收益信息成功").token("").res(selectXiaJiShouYi).data(null);
    }
}
